package cn.hh.user.controller;

import cn.hh.basic.exception.BusinessException;
import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.PageList;
import cn.hh.user.domain.User;

import cn.hh.user.dto.RegisterDto;
import cn.hh.user.query.UserQuery;
import cn.hh.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：用户
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户接口", description = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 接口：查询单个对象
     *
     * @param id
     * @return
     */
    //http://localhost:8080/user/1 + get
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id") Long id) {
        return userService.findOne(id);
    }

    /**
     * 接口：查询所有对象
     *
     * @return
     */
    //http://localhost:8080/user + 无 + get
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    public List<User> findAll() {
        return userService.findAll();
    }

    /**
     * 接口：分页查询
     *
     * @param query
     * @return
     */
    //http://localhost:8080/user + {json对象} + post
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryPage(query);
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    //http://localhost:8080/user/1 + delete
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            userService.del(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 接口：添加或修改
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "添加或修改")
    //http://localhost:8080/user + {json对象} + put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user) {
        try {
            if (user.getId() == null) {//添加
                userService.add(user);
            } else {
                userService.update(user);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }


    /**
     * 接口：批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            //userService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 手机验证码注册接口
     *
     * @param
     * @return
     */
    @PostMapping("/phone/Register")
    public AjaxResult phoneRegister(@RequestBody RegisterDto dto) {
        try {
            userService.phoneRegister(dto);
            return AjaxResult.me();
        } catch (BusinessException businessException) {
            businessException.printStackTrace();
            return AjaxResult.me().setMsg(businessException.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙,请稍后重试");
        }
    }

    /**
     * 邮箱注册接口
     *
     * @param
     * @return
     */
    @PostMapping("/email/Register")
    public AjaxResult emailRegister(@RequestBody RegisterDto dto) {
        try {
            userService.emailRegister(dto);
            return AjaxResult.me();
        } catch (BusinessException businessException) {
            businessException.printStackTrace();
            return AjaxResult.me().setMsg(businessException.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙,请稍后重试");
        }
    }

}